options(warn=-1)
library(tidyr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(rworldmap)
## Loading required package: sp
## ### Welcome to rworldmap ###
## For a short introduction type :   vignette('rworldmap')
library(maps)
library(ggmap)
## 
## Attaching package: 'ggmap'
## The following object is masked from 'package:plotly':
## 
##     wind
library(reshape2)
## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths
library(raster)
## 
## Attaching package: 'raster'
## The following object is masked from 'package:plotly':
## 
##     select
## The following object is masked from 'package:dplyr':
## 
##     select
## The following object is masked from 'package:tidyr':
## 
##     extract
library(rgdal)
## rgdal: version: 1.2-8, (SVN revision 663)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.1.3, released 2017/20/01
##  Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rgdal/gdal
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rgdal/proj
##  Linking to sp version: 1.2-4
library(rgeos)
## rgeos version: 0.3-23, (SVN revision 546)
##  GEOS runtime version: 3.6.1-CAPI-1.10.1 r0 
##  Linking to sp version: 1.2-4 
##  Polygon checking: TRUE
terr = read.csv('~/Downloads/globalterrorismdb_0617dist.csv', check.names = FALSE, header = TRUE, stringsAsFactors = FALSE)
summary(terr)
##     eventid              iyear          imonth            iday      
##  Min.   :1.970e+11   Min.   :1970   Min.   : 0.000   Min.   : 0.00  
##  1st Qu.:1.990e+11   1st Qu.:1990   1st Qu.: 4.000   1st Qu.: 8.00  
##  Median :2.007e+11   Median :2007   Median : 6.000   Median :15.00  
##  Mean   :2.002e+11   Mean   :2002   Mean   : 6.474   Mean   :15.47  
##  3rd Qu.:2.014e+11   3rd Qu.:2014   3rd Qu.: 9.000   3rd Qu.:23.00  
##  Max.   :2.017e+11   Max.   :2016   Max.   :12.000   Max.   :31.00  
##                                                                     
##   approxdate           extended        resolution           country      
##  Length:170350      Min.   :0.00000   Length:170350      Min.   :   4.0  
##  Class :character   1st Qu.:0.00000   Class :character   1st Qu.:  75.0  
##  Mode  :character   Median :0.00000   Mode  :character   Median :  98.0  
##                     Mean   :0.04363                      Mean   : 132.5  
##                     3rd Qu.:0.00000                      3rd Qu.: 160.0  
##                     Max.   :1.00000                      Max.   :1004.0  
##                                                                          
##  country_txt            region        region_txt         provstate        
##  Length:170350      Min.   : 1.000   Length:170350      Length:170350     
##  Class :character   1st Qu.: 5.000   Class :character   Class :character  
##  Mode  :character   Median : 6.000   Mode  :character   Mode  :character  
##                     Mean   : 7.091                                        
##                     3rd Qu.:10.000                                        
##                     Max.   :12.000                                        
##                                                                           
##      city              latitude        longitude         specificity   
##  Length:170350      Min.   :-53.16   Min.   :-176.176   Min.   :1.000  
##  Class :character   1st Qu.: 11.26   1st Qu.:   2.396   1st Qu.:1.000  
##  Mode  :character   Median : 31.47   Median :  43.130   Median :1.000  
##                     Mean   : 23.40   Mean   :  26.351   Mean   :1.454  
##                     3rd Qu.: 34.74   3rd Qu.:  68.451   3rd Qu.:1.000  
##                     Max.   : 74.63   Max.   : 179.367   Max.   :5.000  
##                     NA's   :4606     NA's   :4606       NA's   :4      
##     vicinity          location           summary              crit1       
##  Min.   :-9.00000   Length:170350      Length:170350      Min.   :0.0000  
##  1st Qu.: 0.00000   Class :character   Class :character   1st Qu.:1.0000  
##  Median : 0.00000   Mode  :character   Mode  :character   Median :1.0000  
##  Mean   : 0.06779                                         Mean   :0.9884  
##  3rd Qu.: 0.00000                                         3rd Qu.:1.0000  
##  Max.   : 1.00000                                         Max.   :1.0000  
##                                                                           
##      crit2           crit3          doubtterr        alternative    
##  Min.   :0.000   Min.   :0.0000   Min.   :-9.0000   Min.   :1.00    
##  1st Qu.:1.000   1st Qu.:1.0000   1st Qu.: 0.0000   1st Qu.:1.00    
##  Median :1.000   Median :1.0000   Median : 0.0000   Median :1.00    
##  Mean   :0.993   Mean   :0.8798   Mean   :-0.5721   Mean   :1.29    
##  3rd Qu.:1.000   3rd Qu.:1.0000   3rd Qu.: 0.0000   3rd Qu.:1.00    
##  Max.   :1.000   Max.   :1.0000   Max.   : 1.0000   Max.   :5.00    
##                                                     NA's   :143719  
##  alternative_txt       multiple         success          suicide       
##  Length:170350      Min.   :0.0000   Min.   :0.0000   Min.   :0.00000  
##  Class :character   1st Qu.:0.0000   1st Qu.:1.0000   1st Qu.:0.00000  
##  Mode  :character   Median :0.0000   Median :1.0000   Median :0.00000  
##                     Mean   :0.1342   Mean   :0.8964   Mean   :0.03387  
##                     3rd Qu.:0.0000   3rd Qu.:1.0000   3rd Qu.:0.00000  
##                     Max.   :1.0000   Max.   :1.0000   Max.   :1.00000  
##                                                                        
##   attacktype1   attacktype1_txt     attacktype2     attacktype2_txt   
##  Min.   :1.00   Length:170350      Min.   :1.00     Length:170350     
##  1st Qu.:2.00   Class :character   1st Qu.:2.00     Class :character  
##  Median :3.00   Mode  :character   Median :2.00     Mode  :character  
##  Mean   :3.22                      Mean   :3.67                       
##  3rd Qu.:3.00                      3rd Qu.:6.00                       
##  Max.   :9.00                      Max.   :9.00                       
##                                    NA's   :164720                     
##   attacktype3     attacktype3_txt      targtype1    targtype1_txt     
##  Min.   :1.00     Length:170350      Min.   : 1.0   Length:170350     
##  1st Qu.:2.00     Class :character   1st Qu.: 3.0   Class :character  
##  Median :6.00     Mode  :character   Median : 4.0   Mode  :character  
##  Mean   :5.11                        Mean   : 8.4                     
##  3rd Qu.:7.00                        3rd Qu.:14.0                     
##  Max.   :7.00                        Max.   :22.0                     
##  NA's   :169976                                                       
##   targsubtype1    targsubtype1_txt      corp1             target1         
##  Min.   :  1.00   Length:170350      Length:170350      Length:170350     
##  1st Qu.: 22.00   Class :character   Class :character   Class :character  
##  Median : 34.00   Mode  :character   Mode  :character   Mode  :character  
##  Mean   : 46.87                                                           
##  3rd Qu.: 74.00                                                           
##  Max.   :111.00                                                           
##  NA's   :9345                                                             
##     natlty1       natlty1_txt          targtype2      targtype2_txt     
##  Min.   :   4.0   Length:170350      Min.   : 1.0     Length:170350     
##  1st Qu.:  83.0   Class :character   1st Qu.: 4.0     Class :character  
##  Median : 102.0   Mode  :character   Median :14.0     Mode  :character  
##  Mean   : 127.7                      Mean   :10.2                       
##  3rd Qu.: 175.0                      3rd Qu.:14.0                       
##  Max.   :1004.0                      Max.   :22.0                       
##  NA's   :1394                        NA's   :160332                     
##   targsubtype2    targsubtype2_txt      corp2             target2         
##  Min.   :  1.00   Length:170350      Length:170350      Length:170350     
##  1st Qu.: 14.00   Class :character   Class :character   Class :character  
##  Median : 14.00   Mode  :character   Mode  :character   Mode  :character  
##  Mean   : 28.37                                                           
##  3rd Qu.: 47.00                                                           
##  Max.   :109.00                                                           
##  NA's   :160749                                                           
##     natlty2       natlty2_txt          targtype3      targtype3_txt     
##  Min.   :   4     Length:170350      Min.   : 1.00    Length:170350     
##  1st Qu.:  92     Class :character   1st Qu.: 3.00    Class :character  
##  Median :  98     Mode  :character   Median :14.00    Mode  :character  
##  Mean   : 132                        Mean   : 9.88                      
##  3rd Qu.: 182                        3rd Qu.:14.00                      
##  Max.   :1004                        Max.   :22.00                      
##  NA's   :160644                      NA's   :169316                     
##   targsubtype3    targsubtype3_txt      corp3             target3         
##  Min.   :  1.00   Length:170350      Length:170350      Length:170350     
##  1st Qu.: 26.00   Class :character   Class :character   Class :character  
##  Median : 67.00   Mode  :character   Mode  :character   Mode  :character  
##  Mean   : 55.07                                                           
##  3rd Qu.: 73.00                                                           
##  Max.   :109.00                                                           
##  NA's   :169389                                                           
##     natlty3       natlty3_txt           gname             gsubname        
##  Min.   :   4.0   Length:170350      Length:170350      Length:170350     
##  1st Qu.:  74.0   Class :character   Class :character   Class :character  
##  Median : 104.0   Mode  :character   Mode  :character   Mode  :character  
##  Mean   : 139.6                                                           
##  3rd Qu.: 182.0                                                           
##  Max.   :1004.0                                                           
##  NA's   :169344                                                           
##     gname2           gsubname2            gname3         
##  Length:170350      Length:170350      Length:170350     
##  Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character  
##                                                          
##                                                          
##                                                          
##                                                          
##   gsubname3            motive           guncertain1      guncertain2    
##  Length:170350      Length:170350      Min.   :0.0000   Min.   :0.00    
##  Class :character   Class :character   1st Qu.:0.0000   1st Qu.:0.00    
##  Mode  :character   Mode  :character   Median :0.0000   Median :0.00    
##                                        Mean   :0.0844   Mean   :0.26    
##                                        3rd Qu.:0.0000   3rd Qu.:1.00    
##                                        Max.   :1.0000   Max.   :1.00    
##                                        NA's   :379      NA's   :168669  
##   guncertain3       individual           nperps            nperpcap     
##  Min.   :0.00     Min.   :0.000000   Min.   :  -99.00   Min.   :-99.00  
##  1st Qu.:0.00     1st Qu.:0.000000   1st Qu.:  -99.00   1st Qu.:  0.00  
##  Median :0.00     Median :0.000000   Median :  -99.00   Median :  0.00  
##  Mean   :0.18     Mean   :0.002483   Mean   :  -63.92   Mean   : -1.48  
##  3rd Qu.:0.00     3rd Qu.:0.000000   3rd Qu.:    1.00   3rd Qu.:  0.00  
##  Max.   :1.00     Max.   :1.000000   Max.   :25000.00   Max.   :406.00  
##  NA's   :170085                      NA's   :71124      NA's   :69499   
##     claimed        claimmode      claimmode_txt          claim2      
##  Min.   :-9.00   Min.   : 1.00    Length:170350      Min.   :-9.00   
##  1st Qu.: 0.00   1st Qu.: 6.00    Class :character   1st Qu.: 0.00   
##  Median : 0.00   Median : 8.00    Mode  :character   Median : 0.00   
##  Mean   : 0.03   Mean   : 7.01                       Mean   : 0.24   
##  3rd Qu.: 0.00   3rd Qu.: 9.00                       3rd Qu.: 1.00   
##  Max.   : 1.00   Max.   :10.00                       Max.   : 1.00   
##  NA's   :66134   NA's   :154102                      NA's   :168733  
##    claimmode2     claimmode2_txt         claim3         claimmode3    
##  Min.   : 1.00    Length:170350      Min.   :0.00     Min.   : 1.00   
##  1st Qu.: 6.00    Class :character   1st Qu.:0.00     1st Qu.: 5.50   
##  Median : 7.00    Mode  :character   Median :0.00     Median : 8.00   
##  Mean   : 7.22                       Mean   :0.45     Mean   : 7.09   
##  3rd Qu.:10.00                       3rd Qu.:1.00     3rd Qu.:10.00   
##  Max.   :10.00                       Max.   :1.00     Max.   :10.00   
##  NA's   :169818                      NA's   :170087   NA's   :170230  
##  claimmode3_txt       compclaim        weaptype1      weaptype1_txt     
##  Length:170350      Min.   :-9.00    Min.   : 1.000   Length:170350     
##  Class :character   1st Qu.:-9.00    1st Qu.: 5.000   Class :character  
##  Mode  :character   Median :-9.00    Median : 6.000   Mode  :character  
##                     Mean   :-6.42    Mean   : 6.429                     
##                     3rd Qu.: 0.00    3rd Qu.: 6.000                     
##                     Max.   : 1.00    Max.   :13.000                     
##                     NA's   :165595                                      
##   weapsubtype1   weapsubtype1_txt     weaptype2      weaptype2_txt     
##  Min.   : 1.00   Length:170350      Min.   : 1.00    Length:170350     
##  1st Qu.: 5.00   Class :character   1st Qu.: 5.00    Class :character  
##  Median :12.00   Mode  :character   Median : 6.00    Mode  :character  
##  Mean   :11.02                      Mean   : 6.74                      
##  3rd Qu.:16.00                      3rd Qu.: 8.00                      
##  Max.   :30.00                      Max.   :13.00                      
##  NA's   :19426                      NA's   :158507                     
##   weapsubtype2    weapsubtype2_txt     weaptype3      weaptype3_txt     
##  Min.   : 1.00    Length:170350      Min.   : 2.00    Length:170350     
##  1st Qu.: 5.00    Class :character   1st Qu.: 5.00    Class :character  
##  Median : 7.00    Mode  :character   Median : 6.00    Mode  :character  
##  Mean   :10.67                       Mean   : 6.87                      
##  3rd Qu.:18.00                       3rd Qu.: 8.00                      
##  Max.   :30.00                       Max.   :13.00                      
##  NA's   :159875                      NA's   :168690                     
##   weapsubtype3    weapsubtype3_txt     weaptype4      weaptype4_txt     
##  Min.   : 1.00    Length:170350      Min.   : 5.00    Length:170350     
##  1st Qu.: 4.00    Class :character   1st Qu.: 5.00    Class :character  
##  Median : 7.00    Mode  :character   Median : 6.00    Mode  :character  
##  Mean   :11.51                       Mean   : 6.24                      
##  3rd Qu.:19.00                       3rd Qu.: 6.00                      
##  Max.   :28.00                       Max.   :12.00                      
##  NA's   :168843                      NA's   :170276                     
##   weapsubtype4    weapsubtype4_txt    weapdetail            nkill         
##  Min.   : 2.00    Length:170350      Length:170350      Min.   :   0.000  
##  1st Qu.: 3.00    Class :character   Class :character   1st Qu.:   0.000  
##  Median : 8.00    Mode  :character   Mode  :character   Median :   0.000  
##  Mean   :10.79                                          Mean   :   2.387  
##  3rd Qu.:16.00                                          3rd Qu.:   2.000  
##  Max.   :28.00                                          Max.   :1500.000  
##  NA's   :170279                                         NA's   :9682      
##     nkillus           nkillter          nwound          nwoundus     
##  Min.   :   0.00   Min.   :  0.00   Min.   :   0.0   Min.   :  0.00  
##  1st Qu.:   0.00   1st Qu.:  0.00   1st Qu.:   0.0   1st Qu.:  0.00  
##  Median :   0.00   Median :  0.00   Median :   0.0   Median :  0.00  
##  Mean   :   0.05   Mean   :  0.48   Mean   :   3.2   Mean   :  0.04  
##  3rd Qu.:   0.00   3rd Qu.:  0.00   3rd Qu.:   2.0   3rd Qu.:  0.00  
##  Max.   :1358.00   Max.   :500.00   Max.   :7366.0   Max.   :751.00  
##  NA's   :64459     NA's   :66663    NA's   :15325    NA's   :64710   
##     nwoundte        property        propextent     propextent_txt    
##  Min.   :  0.0   Min.   :-9.000   Min.   :1.0      Length:170350     
##  1st Qu.:  0.0   1st Qu.: 0.000   1st Qu.:3.0      Class :character  
##  Median :  0.0   Median : 1.000   Median :3.0      Mode  :character  
##  Mean   :  0.1   Mean   :-0.506   Mean   :3.3                        
##  3rd Qu.:  0.0   3rd Qu.: 1.000   3rd Qu.:4.0                        
##  Max.   :200.0   Max.   : 1.000   Max.   :4.0                        
##  NA's   :68749                    NA's   :109805                     
##    propvalue          propcomment          ishostkid       
##  Min.   :       -99   Length:170350      Min.   :-9.00000  
##  1st Qu.:       -99   Class :character   1st Qu.: 0.00000  
##  Median :       -99   Mode  :character   Median : 0.00000  
##  Mean   :    229057                      Mean   : 0.05918  
##  3rd Qu.:      2500                      3rd Qu.: 0.00000  
##  Max.   :2700000000                      Max.   : 1.00000  
##  NA's   :134863                          NA's   :178       
##     nhostkid          nhostkidus         nhours           ndays        
##  Min.   :  -99.00   Min.   :-99.00   Min.   :-99.00   Min.   : -99.00  
##  1st Qu.:    1.00   1st Qu.:  0.00   1st Qu.:-99.00   1st Qu.: -99.00  
##  Median :    2.00   Median :  0.00   Median :  0.00   Median : -99.00  
##  Mean   :    4.51   Mean   : -0.38   Mean   :-42.75   Mean   : -31.89  
##  3rd Qu.:    4.00   3rd Qu.:  0.00   3rd Qu.:  0.00   3rd Qu.:   4.00  
##  Max.   :17000.00   Max.   : 86.00   Max.   :999.00   Max.   :2454.00  
##  NA's   :157858     NA's   :157913   NA's   :166649   NA's   :162942   
##     divert          kidhijcountry          ransom     
##  Length:170350      Length:170350      Min.   :-9.00  
##  Class :character   Class :character   1st Qu.: 0.00  
##  Mode  :character   Mode  :character   Median : 0.00  
##                                        Mean   :-0.14  
##                                        3rd Qu.: 0.00  
##                                        Max.   : 1.00  
##                                        NA's   :94037  
##    ransomamt           ransomamtus          ransompaid      
##  Min.   :       -99   Min.   :      -99   Min.   :     -99  
##  1st Qu.:         0   1st Qu.:        0   1st Qu.:     -99  
##  Median :     14200   Median :        0   Median :       0  
##  Mean   :   3224502   Mean   :   451992   Mean   :  384966  
##  3rd Qu.:    400000   3rd Qu.:        0   3rd Qu.:     736  
##  Max.   :1000000000   Max.   :132000000   Max.   :41000000  
##  NA's   :169071       NA's   :169854      NA's   :169643    
##   ransompaidus      ransomnote        hostkidoutcome   hostkidoutcome_txt
##  Min.   :  -99.0   Length:170350      Min.   :1.00     Length:170350     
##  1st Qu.:    0.0   Class :character   1st Qu.:2.00     Class :character  
##  Median :    0.0   Mode  :character   Median :4.00     Mode  :character  
##  Mean   :  272.5                      Mean   :4.62                       
##  3rd Qu.:    0.0                      3rd Qu.:7.00                       
##  Max.   :48000.0                      Max.   :7.00                       
##  NA's   :169863                       NA's   :160439                     
##    nreleased         addnotes            scite1         
##  Min.   : -99.00   Length:170350      Length:170350     
##  1st Qu.: -99.00   Class :character   Class :character  
##  Median :   0.00   Mode  :character   Mode  :character  
##  Mean   : -28.72                                        
##  3rd Qu.:   1.00                                        
##  Max.   :1201.00                                        
##  NA's   :161028                                         
##     scite2             scite3            dbsource            INT_LOG      
##  Length:170350      Length:170350      Length:170350      Min.   :-9.000  
##  Class :character   Class :character   Class :character   1st Qu.:-9.000  
##  Mode  :character   Mode  :character   Mode  :character   Median :-9.000  
##                                                           Mean   :-4.583  
##                                                           3rd Qu.: 0.000  
##                                                           Max.   : 1.000  
##                                                                           
##     INT_IDEO         INT_MISC           INT_ANY         related         
##  Min.   :-9.000   Min.   :-9.00000   Min.   :-9.000   Length:170350     
##  1st Qu.:-9.000   1st Qu.: 0.00000   1st Qu.:-9.000   Class :character  
##  Median :-9.000   Median : 0.00000   Median : 0.000   Mode  :character  
##  Mean   :-4.511   Mean   : 0.09108   Mean   :-3.975                     
##  3rd Qu.: 0.000   3rd Qu.: 0.00000   3rd Qu.: 0.000                     
##  Max.   : 1.000   Max.   : 1.00000   Max.   : 1.000                     
## 
c(ncol(terr),nrow(terr))
## [1]    135 170350
sum(is.na(terr))#/(ncol(data)*nrow(data))
## [1] 6322260
terr=rename(terr, id=eventid, year=iyear, nation=country_txt, 
            Region=region_txt, attack=attacktype1_txt,
            target=targtype1_txt, weapon=weaptype1_txt, 
            Killed=nkill, wounded=nwound)

0.1 Data cleaning

We clean the data

terr$Killed=as.integer(terr$Killed)
terr$wounded=as.integer(terr$wounded)

terr$Killed[which(is.na(terr$Killed))] = 0
terr$wounded[which(is.na(terr$wounded))] = 0


terr$nation[terr$nation=="United States"] <- "USA"
terr$nation[terr$nation=="United Kingdom"] <- "UK"
terr$nation[terr$nation=="People's Republic of the Congo"] <- "Republic of Congo"
terr$nation[terr$nation=="Bosnia-Herzegovina"] <- "Bosnia and Herzegovina"
terr$nation[terr$nation=="Slovak Republic"] <- "Slovakia"
global_t <- 
  terr %>%
  group_by(year,nation,Region) %>%
  summarize(Total=n())

global_y <- global_t%>%group_by(year)%>%summarize(Total=sum(Total))
global_attacks <- 
  global_t %>%
  group_by(nation) %>%
  summarize(Total=sum(Total)) %>% 
  arrange(desc(Total))
#%>%filter(Total>300)
#global_attacks$Total[which(is.na(global_attacks$Total))] = 0

attach(global_attacks)
global_n <- global_attacks[order(-Total),]
detach(global_attacks)

Let’s look at the number of terrorist attacks with the passage of time.

gy <- global_y %>%
  ggplot(mapping=aes(year,Total))+
  geom_line(color="red")+
  #geom_bar(mapping=aes(x=year,y=Total,fill=year), stat='identity')+
  theme(legend.position="none", panel.background = NULL, axis.text.x = element_text(angle=45, vjust = 1))+
  labs(x="Year", y="Number of attacks", title="Number of global attacks over years")
ggplotly(gy, width = 800, height=480)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`
global_kills_years <- terr%>%group_by(year)%>%summarize(killed=sum(Killed))
global_wound_years <- terr%>%group_by(year)%>%summarize(wounded=sum(wounded))
globe <- 
  global_kills_years %>% 
  inner_join(global_wound_years, by="year")
#global_kills_years <- global_kills_years%>%group_by(year)%>%summarize(Total=sum(Total))
#head(globe)

df <- melt(globe, "year")
df=rename(df, effect=variable)

gky <- df %>%
  ggplot(mapping=aes(x=year,y=value, color=effect))+
  geom_line()+
  theme(panel.background = NULL, axis.text.x = element_text(angle=45, vjust = 1))+
  labs(x="Year", y="Count", title="Number of people killed/wounded over years")
ggplotly(gky, width = 800, height=450)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`
world <- map_data("world")
global <- rename(terr, long=longitude, lat=latitude)
#helper theme for common visualizations
ditch_the_axes <- theme(
  axis.text = element_blank(),
  axis.line = element_blank(),
  axis.ticks = element_blank(),
  panel.border = element_blank(),
  panel.grid = element_blank(),
  axis.title = element_blank()
  )
#helper function to visualize maps for varaibles for counts
visualize_count_map <- function(global_table, title){
  global_table <- 
    global_table %>%
    rename(region=nation)

  #join the data found with world data
  world_table <- right_join(global_table, world, by="region")
  world_table$Total[which(is.na(world_table$Total))]=0
  
  g <- ggplot(data = world, aes(x = long, y = lat, group = group)) + 
  coord_fixed(1.3) +
  ditch_the_axes+
  theme(legend.position = "none", panel.background = NULL)+
  geom_polygon(data = world_table, aes(label = region, fill = Total), color ="white") +
  scale_fill_gradientn(colours=c("#e1eec3","#f05053"))+
  ggtitle(title)

  ggplotly(g, width = 850, height = 480)
}

1 Let’s look at the number of terrorist attacks across the globe.

visualize_count_map(global_attacks, "Number of terrorist attacks around the world")
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`

2 Let’s look at the number of people killed across the global in terrorist atttacks.

global_kills <- 
  global %>% 
  group_by(nation) %>% 
  summarise(Total = sum(Killed))

visualize_count_map(global_kills, "Number of people killed around the world")
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`
global_wound <- 
  global %>% 
  group_by(nation) %>% 
  summarise(Total = sum(wounded))

visualize_count_map(global_wound, "Number of people wounded around the world")
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`

3 Let’s look at the type of weapons used the most in terrorist attacks across the globe.

#get weapon most used in each nation
global_weapons_max_type <- 
  terr[c('nation', 'weapon')] %>% 
  group_by(nation, weapon) %>%
  summarize(Total=n()) %>%
  filter(Total==max(Total)) %>%
  group_by(nation)

global_weapons_max_type <- 
  global_weapons_max_type %>%
  rename(region=nation)

#join the data found with world data
world_weapons <- right_join(global_weapons_max_type, world, by="region")
world_weapons$weapon[which(is.na(world_weapons$weapon))]="Unknown"
num_unique_weapons <- length(unique(world_weapons$weapon))
                             
g4 <- ggplot(data = world, aes(x = long, y = lat, group = group)) + 
  coord_fixed(1.3) +
  ditch_the_axes+
  theme(panel.background = NULL)+
  geom_polygon(data = world_weapons, aes(label = region, fill = factor(weapon)), color ="white") +
  scale_fill_manual(values = c(rainbow(num_unique_weapons, 
                                       s = 0.6, 
                                       v = 0.8, start = 0, 
                                       end = max(1, num_unique_weapons - 1)/num_unique_weapons, 
                                       alpha = 0.7)))+
  ggtitle("Most used weapons in terrorist attacks around the world")

ggplotly(g4, width = 850, height = 480)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`
visualize_factor_map <- function(global_table, factor, title) {
  global_attack_max_type <- 
    global_attack_max_type %>%
    rename(region=nation)

  #join the data found with world data
  world_attack_types <- right_join(global_attack_max_type, world, by="region")
  world_attack_types$attack[which(is.na(world_attack_types$attack))]="Unknown"
  num_unique_attacks <- length(unique(global_attack_max_type$attack))
                             
  g5 <- ggplot(data = world, aes(x = long, y = lat, group = group)) + 
    coord_fixed(1.3) +
    ditch_the_axes+
    theme(panel.background = NULL)+
    geom_polygon(data = world_attack_types, aes(label = region, fill = factor(attack)), color ="white") +
    scale_fill_manual(values = c(rainbow(num_unique_attacks, 
                                       s = 0.6, 
                                       v = 0.8, start = 0, 
                                       end = max(1, num_unique_attacks - 1)/num_unique_attacks, 
                                       alpha = 0.7)))+
  ggtitle("Most particular type of attacks in different countries")

ggplotly(g5, width = 900, height = 480)
}

4 Let’s look at the most particular type of attacks in different countries.

#get weapon most used in each nation
global_attack_max_type <- 
  terr[c('nation', 'attack')] %>% 
  group_by(nation, attack) %>%
  summarize(Total=n()) %>%
  filter(Total==max(Total)) %>%
  group_by(nation)

global_attack_max_type <- 
  global_attack_max_type %>%
  rename(region=nation)

#join the data found with world data
world_attack_types <- right_join(global_attack_max_type, world, by="region")
world_attack_types$attack[which(is.na(world_attack_types$attack))]="Unknown"
num_unique_attacks <- length(unique(global_attack_max_type$attack))
                             
g5 <- ggplot(data = world, aes(x = long, y = lat, group = group)) + 
  coord_fixed(1.3) +
  ditch_the_axes+
  theme(panel.background = NULL)+
  geom_polygon(data = world_attack_types, aes(label = region, fill = factor(attack)), color ="white") +
  scale_fill_manual(values = c(rainbow(num_unique_attacks, 
                                       s = 0.6, 
                                       v = 0.8, start = 0, 
                                       end = max(1, num_unique_attacks - 1)/num_unique_attacks, 
                                       alpha = 0.7)))+
  ggtitle("Most particular type of attacks in different countries")

ggplotly(g5, width = 900, height = 480)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`

5 Let’s look at the most attacked targets in different countries

#get weapon most used in each nation
global_target_max_type <- 
  terr[c('nation', 'target')] %>% 
  group_by(nation, target) %>%
  summarize(Total=n()) %>%
  filter(Total==max(Total)) %>%
  group_by(nation)

global_target_max_type <- 
  global_target_max_type %>%
  rename(region=nation)

#join the data found with world data
world_target_types <- right_join(global_target_max_type, world, by="region")
world_target_types$target[which(is.na(world_target_types$target))]="Unknown"
num_unique_targets <- length(unique(global_target_max_type$target))
                             
g6 <- ggplot(data = world, aes(x = long, y = lat, group = group)) + 
  coord_fixed(1.3) +
  ditch_the_axes+
  theme(panel.background = NULL)+
  geom_polygon(data = world_target_types, aes(label = region, fill = factor(target)), color ="white") +
  scale_fill_manual(values=c(terrain.colors(num_unique_targets, alpha = 0.8)))+
  ggtitle("Most particular type of targets in different countries")

ggplotly(g6, width = 900, height = 480)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`

6 Most active perpetrator groups around the world

#get weapon most used in each nation
global_group_max_attacks <- 
  terr[c('nation', 'gname')] %>% 
  group_by(nation, gname) %>%
  summarize(Total=n()) %>%
  filter(gname!= "Unknown") %>%
  filter(Total==max(Total)) %>%
  group_by(nation)

global_group_max_attacks <- 
  global_group_max_attacks %>%
  rename(region=nation)

#join the data found with world data
world_groups_attacks <- right_join(global_group_max_attacks, world, by="region")
world_groups_attacks$gname[which(is.na(world_groups_attacks$gname))]="Unknown"
num_unique_groups <- length(unique(global_group_max_attacks$gname))
terrorist_group <- factor(world_groups_attacks$gname)
                             
g7 <- ggplot(data = world, aes(x = long, y = lat, group = group)) + 
  coord_fixed(1.3) +
  ditch_the_axes+
  theme(legend.position = "none", panel.background = NULL)+
  geom_polygon(data = world_groups_attacks, aes(label = region, fill = terrorist_group), color ="white") +
  scale_fill_manual(values=c(topo.colors(num_unique_groups, alpha = 0.6)))+
  ggtitle("Most active terrorist groups around the globe")

ggplotly(g7, width = 850, height = 480)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`

7 Taking a look at the international attacks

#get weapon most used in each nation
global_int_attacks <- 
  terr[c('nation', 'INT_ANY')] %>% 
  filter(INT_ANY=='1') %>%
  group_by(nation) %>%
  summarize(Total=n())

visualize_count_map(global_int_attacks, "International attacks around the globe")
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`

8 Taking a look at the domestic attacks

#get weapon most used in each nation
global_dom_attacks <- 
  terr[c('nation', 'INT_ANY')] %>% 
  filter(INT_ANY=='0') %>%
  group_by(nation) %>%
  summarize(Total=n())

visualize_count_map(global_dom_attacks, "Domestic attacks around the globe")
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`

9 attacks by highest casualties(killed+wounded)

#get weapon most used in each nation
terr$casualties=as.integer(terr$Killed+terr$wounded)
terr$casualties[which(is.na(terr$casualties))]=0
g_max_cas <- terr%>%
  top_n(10, casualties) %>%
  ggplot(mapping=aes(x=reorder(target1, -casualties), y=casualties, fill=target1)) +
  geom_bar(stat = 'identity')+
  theme(legend.position="none", panel.background = NULL, axis.text.x =  element_text(angle=50, vjust = 1))+
  labs(x="Target of attack", y="Number of casulaties", title="Terrorist attacks with most casualties")
ggplotly(g_max_cas)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`

Let’s look at the 40 countries with maximum number of terrorist attacks, and 40 countries with least number of terrorist attacks

g2 <- global_n%>%
  top_n(40) %>%
  ggplot(mapping=aes(x=reorder(nation, -Total),y=Total,fill=nation)) + 
  geom_bar(stat='identity')+
  theme(legend.position="none", panel.background = NULL, axis.text.x = element_text(angle=90, vjust = 1))+
  labs(x="Countries", y="Number of attacks", title="Countries with most number of terrorist attacks")
## Selecting by Total
ggplotly(g2, width = 800, height=450)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`
g2 <- global_n%>%
  top_n(-40)%>%
  ggplot(mapping=aes(x=reorder(nation, Total),y=Total,fill=nation)) + 
  geom_bar(stat='identity')+
  theme(legend.position="none", panel.background = NULL, axis.text.x = element_text(angle=90, vjust = 1))+
  labs(x="Countries", y="Number of attacks", title="Countries with least number of terrorist attacks")
## Selecting by Total
ggplotly(g2, width = 800, height=450)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`